//
//  WebViewController.m
//  Alarm_Clock_UserNotifications
//
//  Created by 健健 on 2018/3/14.
//  Copyright © 2018年 欧阳荣. All rights reserved.
//

#import "WebViewController.h"
#import "BtmView.h"
#import "ProgressHUD.h"
#define mScreen_W [UIScreen mainScreen].bounds.size.width
#define mScreen_H [UIScreen mainScreen].bounds.size.height

#define IphoneX_H  mScreen_H == 812.f
#define TabBar_H   (IphoneX_H ? 44.f+ 34.f : 44.f)
#define URL   @"http://www.6588695.com"

@interface WebViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)BtmView * btmView;

@property(nonatomic,assign) BOOL isLoadOrNot;
@end

@implementation WebViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];

    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backNotification) name:@"backNotification" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(forwardNotification) name:@"forwardNotification" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(refreshNotification) name:@"refreshNotification" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(stopNotification) name:@"stopNotification" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(homeNotification) name:@"homeNotification" object:nil];
   
    [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isGuide"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    _webView = [[UIWebView alloc]init];
    _webView.frame = CGRectMake(0, IphoneX_H ? 54 : 20, mScreen_W, mScreen_H-TabBar_H - (IphoneX_H ? 54 : 20));
    NSURL *url = [NSURL URLWithString:URL];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    _webView.delegate = self;
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    
    self.btmView = [[BtmView alloc]init];
    self.btmView.frame = CGRectMake(0, CGRectGetMaxY(_webView.frame), mScreen_W, TabBar_H);
    [self.view addSubview:self.btmView];
    
    [self refreshButtonsState];
    
}
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}


- (void)backNotification {
    if (_webView.canGoBack) {
        [_webView goBack];
    }
}
- (void)forwardNotification {
    if (_webView.canGoForward) {
        [_webView goForward];
    }
}
- (void)refreshNotification {
    [_webView reload];
}
- (void)stopNotification {
    [ProgressHUD dismiss];
   [_webView stopLoading];
}
- (void)homeNotification {
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URL]]];
}


#pragma mark - WebView Delegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
    [ProgressHUD showLoading];
    self.btmView.refreshBtn.selected = YES;
    self.isLoadOrNot = YES;
    [self refreshButtonsState];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [ProgressHUD dismiss];
    self.isLoadOrNot = NO;
    self.btmView.refreshBtn.selected = NO;
    [self refreshButtonsState];
}
- (void)refreshButtonsState {
    if (_webView.canGoBack) {
        [self.btmView.backBtn setEnabled:YES];
    } else {
        [self.btmView.backBtn setEnabled:NO];
    }
    if (_webView.canGoForward) {
        [self.btmView.forwardBtn setEnabled:YES];
    } else {
        [self.btmView.forwardBtn setEnabled:NO];
    }

}

@end
